
#include "GameManager.h"
#include "Weapon.h"
#include "MemoryStream.h"
#include "Projectile.h"
#include "NavigationManager.h"

#include "NuclearMissile.h"
#include "LaserheadMissile.h"
#include "CounterMissile.h"

CWeapon::CWeapon(CSceneObject *parent) : TRefCount<CWeapon>()
{
	this->mParent = parent;
	this->misFiring = false;
}

CWeapon::~CWeapon(void)
{
}
void CWeapon::passTime(f32 delta) {
	if(this->isFiring()) {
		DEBUG_PRINT("Fire + targeted fire at %08x\n", this->mParent->getTarget()->getID());
		
		// temp, put missle 1000 in front of us to stop self collision
		matrix4 m;
		vector3df vel = vector3df(0,0,1000);
		m.setRotationDegrees(this->getVelocity());
		m.transformVect(vel);
	
		// test, create a bunch of projectiles and launch them off.
		// uhh...create a bunch of circles and throw it at the target...
		CGameManager *engine = CGameManager::getInstance();
		CProjectile *obj;
		
		if(!stricmp(this->mProjectile->getClass(), "PROJECTILECLASS_NUCLEARMISSILE")) {
			CNuclearMissile *t = new CNuclearMissile();
			obj = (CProjectile *)t;
		}  else {
			obj = new CProjectile();
		}

		this->mProjectile->copy(obj);
		
		vector3df position = this->mParent->getPosition() + vel;
		obj->setPosition(position);
		obj->setTarget(this->mParent->getTarget());
		
		// plot intercept and fire
		CNavigationManager *nav = CNavigationManager::getInstance();
		std::vector<vector3df> intercept;
		std::vector<f32> times;
		
		if(nav->getInterceptCourse(obj, obj->getTarget(), intercept, times, obj->getMaxNormalAcceleration()))
			obj->setRotation(intercept[0]);

		obj->setAcceleration(obj->getMaxNormalAcceleration() );
		engine->addObject(obj);
		
		this->misFiring = false;
	}
}

void CWeapon::fire() {
	if(this->mParent->getTarget()) {
		this->misFiring = true;
	}
}

CWeapon *CWeapon::copy(void) {
	CWeapon *ptr = new CWeapon(this->mParent);
	ptr->setMountPoint(this->mMountPoint);
	ptr->setXmlID(this->getXmlID());
	ptr->setName(this->getName());
	ptr->setClass(this->getClass());
	ptr->setParent(this->getParent());
	ptr->setProjectile(this->getProjectile());
	ptr->setProjectileID(this->getProjectileID());
	return ptr;
}
